
package com.itheima.entry;

import com.itheima.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

/*
程序的入口
 */
public class Entry {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        ArrayList<Student> list = new ArrayList<>();
        //菜单展示
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            //键盘录入
            String choice = sc.nextLine();
            //用户选择   调用方法 干活
            switch (choice) {
                case "1":
                    addStudent(list);
                    break;
                case "2":
                    deleteStudent(list);
                    break;
                case "3":
                    updateStudent(list);
                    break;
                case "4":
                    findAllStudents(list);
                    break;
                case "5":
                    return;
                default:
                    break;
            }
        }
        //死循环


    }

    private static void findAllStudents(ArrayList<Student> list) {
        if (list==null||list.size()==0){
            System.out.println("没有学生,请先添加");
            return;
        }
        System.out.println("学号\t姓名\t年龄\t生日");
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            System.out.println(student.getSid()+"\t"+student.getName()+"\t"+student.getAge()+"岁\t"+student.getBirthday());
        }


    }

    private static void updateStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        String sid = null;
        while (true) {
            System.out.println("请输入学号");
            sid = sc.next();
            int index = getIndex(sid, list);
            if (index>=0){
                System.out.println("请输入姓名");
                String name = sc.next();

                System.out.println("请输入年龄");
                int age= sc.nextInt();
                System.out.println("请输入生日");
                String birthday = sc.next();
                Student student = new Student(sid, name, age, birthday);
                list.set(index,student);
                System.out.println("修改成功");
                break;
            }else {
                System.out.println("不存在,请重新输入");
            }
        }
    }

    private static void deleteStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        String sid = null;
        while (true) {
            System.out.println("请输入学号");
            sid = sc.next();
            int index = getIndex(sid, list);
            if (index>=0){
                list.remove(index);
                System.out.println("删除成功");
                break;
            }else {
                System.out.println("不存在,请重新输入");
            }
        }


    }

    private static void addStudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        String sid = null;
        while (true) {
            System.out.println("请输入学号");
            sid = sc.next();
            int index = getIndex(sid, list);
            if (index<0){
                break;
            }else {
                System.out.println("已经存在,请重新输入");
            }
        }
        System.out.println("请输入姓名");
        String name = sc.next();

        System.out.println("请输入年龄");
        int age= sc.nextInt();
        System.out.println("请输入生日");
        String birthday = sc.next();
        Student student = new Student(sid, name, age, birthday);
        list.add(student);
        System.out.println("添加成功");

    }

    //获取学号所在的索引
    public static int getIndex(String sid ,ArrayList<Student> list){
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            if(student.getSid().equals(sid)){
                return i;
            }
        }
        return -1;
    }

}
